<#include "../base/base.ftl">
<@body>
<div class="wu-toolbar-search" id="tb">
    <a href="#" id="refresh" class="easyui-linkbutton" data-options="iconCls:'icon fa fa-refresh',plain:true">刷新</a>
    <a href="#" id="add" class="easyui-linkbutton" data-options="iconCls:'icon fa fa-plus',plain:true">新增</a>
    <a href="#" id="edit" class="easyui-linkbutton" data-options="iconCls:'icon fa fa-pencil-square-o',plain:true">编辑</a>
    <a href="#" id="del" class="easyui-linkbutton" data-options="iconCls:'icon fa fa-minus-circle',plain:true">删除</a>
</div>
    <table id="tt" >
    </table>
</@body>
<@js>
<script type="text/javascript">
    $(function(){
        $('#tt').treegrid({
            idField:'id',
            fit:true,
            rownumbers:true,
            treeField:'name',
            fitColumns:true,
            toolbar:'#tb',
            onDblClickRow: function(index,field,value){
                $("#edit").click();
            },
            url:'/category/getAllData',
            columns:[[
                {field:'name',title:'名称',width:100,align: 'left'},
                {field:'remark',title:'备注',width:100,align:'center'}
            ]],
            onLoadSuccess: function (row, data)
            {

            },
            loadFilter: function(rows){
                return convertTreeGrid(rows);
            }
        });
        /**
         *刷新按钮
         */
        $("#refresh").click(function () {
            $("#tt").treegrid('reload',{});
        });

        /**
         * 新增按钮
         */
        $("#add").click(function () {
            $("<div></div>").dialog({
                id:'addView',
                title:'新增分类',
                resizable: true,
                closable: true,
                width: 400,
                height: 300,
                modal:true,
                href: "/category/view",
                onClose:function () {
                    $("#addView").dialog('destroy');
                },
                onDestroy: function () {
                    $("#tt").treegrid('reload');
                },
                buttons:[{
                    iconCls: 'icon fa fa-check',
                    text: '保存',
                    handler: function () {
                        var index=com.msg_load();
                        $.ajax({
                            type: "POST",
                            url: "/category/save",
                            data:$("#editForm").serialize(),
                            success: function (data) {
                                com.msg_load_close(index);
                                var d=eval(data);
                                if (data!=null&&d.msg=='success') {
                                    $("#addView").dialog('close');
                                    $("#tt").treegrid('reload');
                                    com.msg_success(d.data);
                                } else {
                                    $.messager.progress('close');
                                    com.msg_error(d.msg);
                                }
                            }
                        });
                    }
                }, {
                    text: '关闭',
                    iconCls: 'icon fa fa-times',
                    handler: function () {
                        $("#addView").dialog('close');
                    }
                }]
            });
        });

        /**
         * 编辑按钮
         */
        $("#edit").click(function () {
            var row=$("#tt").treegrid('getSelected');
            if (row==null){
                com.msg_error("请选择要操作的行");
                return false;
            }
            var id=row.id;
            $("<div></div>").dialog({
                id:'view',
                title:'编辑分类',
                resizable: true,
                closable: true,
                width: 400,
                height: 300,
                modal:true,
                href: "/category/view?id="+id,
                onClose:function () {
                    $("#view").dialog('destroy');
                },
                onDestroy: function () {
                    $("#tt").treegrid('reload');
                },
                buttons:[{
                    iconCls: 'icon fa fa-check',
                    text: '保存',
                    handler: function () {
                        var index=com.msg_load();
                        $.ajax({
                            type: "POST",
                            url: "/category/save",
                            data:$("#editForm").serialize(),
                            success: function (data) {
                                com.msg_load_close(index);
                                var d=eval(data);
                                if (data!=null&&d.msg=='success') {
                                    $("#view").dialog('close');
                                    $("#tt").treegrid('reload');
                                    com.msg_success(d.data);
                                } else {
                                    $.messager.progress('close');
                                    com.msg_error(d.msg);
                                }
                            }
                        });
                    }
                }, {
                    text: '关闭',
                    iconCls: 'icon fa fa-times',
                    handler: function () {
                        $("#view").dialog('close');
                    }
                }]
            });
        });

        /**
         * 删除按钮
         */
        $("#del").click(function () {
            var row=$("#tt").treegrid('getSelected');
            if (row==null){
                com.msg_error("请选择要操作的行");
                return false;
            };
            var id=row.id;
            com.msg_confirm("确认要删除该分类吗？",function () {
                $.ajax({
                    type: 'POST',
                    url: "/category/del",
                    data: {id: id},
                    success: function (data) {
                        var d = eval(data);
                        if (data != null && d.msg == 'success') {
                            $("#tt").treegrid('reload');
                            com.msg_success(d.data);
                        } else {
                            com.msg_error(d.msg);
                        }
                    }
                })
            })
        })
    });
</script>
</@js>